﻿namespace mVids
{
    using MediaInfoLib;
    using MediaPortal.Configuration;
    using MediaPortal.GUI.Library;
    using MediaPortal.Playlists;
    using MediaPortal.Profile;
    //using MediaPortal.ServiceImplementations;
    using myLog;
    using SQLite.NET;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.IO;
    using System.Net;
    using System.Runtime.InteropServices;
    using System.Threading;

    internal class database
    {
        public ManualResetEvent[] backgroundImageItems = new ManualResetEvent[0];
        public BackgroundWorker bgw;
        private mVids.multipleThreadedChoice choiceDialog = new mVids.multipleThreadedChoice();
        public SQLiteClient sqc = new SQLiteClient(Config.GetFolder(Config.Dir.Database) + @"\mvidV2.db3");
        private int thumbHeight;

        public bool Add(List<mVids.musicVideo> rawVideos)
        {
            Settings settings = new Settings(Config.GetFolder(Config.Dir.Config) + "/MediaPortal.xml", false);
            this.thumbHeight = settings.GetValueAsInt("mvid", "thumbheight", 350);
            settings.Dispose();
            SQLiteResultSet set = new SQLiteResultSet();
            mVids.lastFM tfm = new mVids.lastFM();
            Log.Debug("Thumb height set to {0}", new object[] { this.thumbHeight.ToString() });
            for (int i = 0; i < rawVideos.Count; i++)
            {
                if ((this.bgw != null) && this.bgw.CancellationPending)
                {
                    this.bgw.ReportProgress(1, "Waiting for image threads to finish");
                    try
                    {
                        this.WaitAll();
                    }
                    catch (Exception exception)
                    {
                        Log.Error("Threading error {0}", new object[] { exception.ToString() });
                    }
                    this.bgw.ReportProgress(1, "Image threads have exited");
                    Log.Debug("Music Video DB - Threads are done, exiting.", new object[0]);
                    break;
                }
                try
                {
                    int num2;
                    double num3;
                    mVids.imgthreadJob userState = new mVids.imgthreadJob();
                    if (!GUIWindowManager.Initalized)
                    {
                        userState.jobNumber = rawVideos[i].jobNum;
                        Log.Debug("Music Video DB - Imgjob for {0} has a jobnum of {1}", new object[] { rawVideos[i].Path, userState.jobNumber.ToString() });
                    }
                    List<string> dbArtists = this.getArtists();
                    Log.Debug("Music Video DB - Beginning addition of video={0}, artist={1}, title={2}, last.fm={3}", new object[] { rawVideos[i].Path, rawVideos[i].artist, rawVideos[i].Label, rawVideos[i].aI.lfmName });
                    rawVideos[i].Label = this.cleanupName(rawVideos[i].Label.Trim().ToLower());
                    string rawName = rawVideos[i].artist.Trim().ToLower();
                    if (rawName.Contains("_"))
                    {
                        rawName = rawName.Replace("_", " ");
                    }
                    if (rawName.Contains(" ft "))
                    {
                        rawName = rawName.Remove(rawName.IndexOf("ft "));
                    }
                    if (rawName.Contains(" ft."))
                    {
                        rawName = rawName.Remove(rawName.IndexOf("ft."));
                    }
                    if (rawName.Contains("(ft"))
                    {
                        rawName = rawName.Remove(rawName.IndexOf("(ft"));
                    }
                    else if (rawName.Contains("feat "))
                    {
                        rawName = rawName.Remove(rawName.IndexOf("feat "));
                    }
                    else if (rawName.Contains("feat."))
                    {
                        rawName = rawName.Remove(rawName.IndexOf("feat."));
                    }
                    rawName = this.cleanupName(rawName);
                    if (rawVideos[i].maskedName)
                    {
                        rawName = rawVideos[i].artist;
                    }
                    string str2 = this.getMatch(rawName);
                    if (str2 == "")
                    {
                        if (rawVideos[i].aI.lfmName == "")
                        {
                            mVids.artistInfo info = tfm.findArtist(this.cleanupName(rawName), dbArtists);
                            if (info == null)
                            {
                                rawVideos[i].aI.lfmName = "";
                            }
                            else
                            {
                                rawVideos[i].aI = info;
                            }
                        }
                        if (!GUIWindowManager.Initalized && (rawVideos[i].aI.lfmName == ""))
                        {
                            if (tfm.getLastfmMatches() != null)
                            {
                                List<mVids.artistInfo> list2 = tfm.getLastfmMatches();
                                userState.type = mVids.imgthreadJob.jobType.multipleChoice;
                                userState.musicVideo = rawVideos[i];
                                userState.artist = rawName;
                                userState.choices = list2;
                                this.bgw.ReportProgress(50, userState);
                                return false;
                            }
                            this.bgw.ReportProgress(0, "No matches found at last.fm for " + rawName);
                            return true;
                        }
                        if (rawVideos[i].aI.lfmName != "")
                        {
                            if (!GUIWindowManager.Initalized)
                            {
                                this.bgw.ReportProgress(1, "Determining duration of " + rawVideos[i].Path);
                            }
                            num2 = 0;
                            num3 = 1.7777;
                            this.getInfo(rawVideos[i].Path, out num2, out num3);
                            if (rawVideos[i].aI.customlfmName)
                            {
                                if (this.resolveLfmName(rawVideos[i].aI.lfmName) != "")
                                {
                                    rawVideos[i].aI.name = this.resolveLfmName(rawVideos[i].aI.lfmName);
                                }
                                else
                                {
                                    rawVideos[i].aI = tfm.getArtistInfo(rawVideos[i].aI);
                                    if (rawVideos[i].aI.notFound)
                                    {
                                        Log.Debug("Music Video DB - supplied last.fm name of {0} was not valid.  Video {1} skipped", new object[] { rawVideos[i].aI.lfmName, rawVideos[i].Path });
                                        return true;
                                    }
                                }
                            }
                            if (rawVideos[i].aI.name == "")
                            {
                                return true;
                            }
                            this.sqc.Execute(string.Concat(new object[] { 
                                "INSERT INTO 'files' (filename,artist,song,playcount,duration,rating,score) VALUES (", '"', rawVideos[i].Path, '"', ",", '"', rawVideos[i].aI.lfmName, '"', ",", '"', rawVideos[i].Label, '"', ",", rawVideos[i].playcount, ",", '"', 
                                num2, '"', ",", rawVideos[i].rating, ",", rawVideos[i].score, ")"
                             }));
                            if (!GUIWindowManager.Initalized)
                            {
                                this.bgw.ReportProgress(1, "Added (song) " + rawVideos[i].aI.name + " - " + rawVideos[i].Label);
                            }
                            if (!GUIWindowManager.Initalized)
                            {
                                userState.type = mVids.imgthreadJob.jobType.thumb;
                                userState.video = rawVideos[i].Path;
                                userState.width = Math.Round((double) (this.thumbHeight * num3), 0);
                            }
                            else
                            {
                                this.getThumb(rawVideos[i].Path, Math.Round((double) (this.thumbHeight * num3), 0));
                            }
                            if (!this.getlfmArtists().Contains(rawVideos[i].aI.lfmName))
                            {
                                if (!rawVideos[i].aI.customlfmName)
                                {
                                    rawVideos[i].aI = tfm.getArtistInfo(rawVideos[i].aI);
                                }
                                if (!this.getlfmArtists().Contains(rawVideos[i].aI.lfmName))
                                {
                                    this.sqc.Execute(string.Concat(new object[] { 
                                        "INSERT INTO 'artists' (artist,lfmname,bio,content,tags,similar) VALUES (", '"', rawVideos[i].aI.name, '"', ",", '"', rawVideos[i].aI.lfmName, '"', ",", '"', rawVideos[i].aI.bioSummary, '"', ",", '"', rawVideos[i].aI.bioContent, '"', 
                                        ",", '"', rawVideos[i].aI.tags, '"', ",", '"', rawVideos[i].aI.similar, '"', ")"
                                     }));
                                    if (rawVideos[i].aI.addMatch)
                                    {
                                        this.addMatch(rawName, rawVideos[i].aI.lfmName);
                                    }
                                    if (!GUIWindowManager.Initalized)
                                    {
                                        this.bgw.ReportProgress(1, "Added (artist) " + rawVideos[i].aI.name);
                                    }
                                    Log.Debug("Music Video DB: " + rawVideos[i].aI.name + " was not found in the database and was added.", new object[0]);
                                    if ((rawVideos[i].aI.imageURL != "") && !System.IO.File.Exists(Config.GetFolder(Config.Dir.Thumbs) + @"\mvids\Artists\" + rawVideos[i].aI.name + ".jpg"))
                                    {
                                        if (!GUIWindowManager.Initalized)
                                        {
                                            userState.type = mVids.imgthreadJob.jobType.both;
                                            userState.aI = (mVids.artistInfo) rawVideos[i].aI.Clone();
                                        }
                                        else
                                        {
                                            this.downloadArtistImage(rawVideos[i].aI);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Log.Debug("Music Video DB - Matched " + rawName.Trim().ToLower() + " to " + str2, new object[0]);
                        if (!GUIWindowManager.Initalized)
                        {
                            this.bgw.ReportProgress(1, "Determining duration of " + rawVideos[i].Path);
                        }
                        num2 = 0;
                        num3 = 1.7777;
                        this.getInfo(rawVideos[i].Path, out num2, out num3);
                        this.sqc.Execute(string.Concat(new object[] { 
                            "INSERT INTO 'files' (filename,artist,song,playcount,duration,rating,score) VALUES (", '"', rawVideos[i].Path, '"', ",", '"', str2, '"', ",", '"', rawVideos[i].Label, '"', ",", rawVideos[i].playcount, ",", '"', 
                            num2, '"', ",", rawVideos[i].rating, ",", rawVideos[i].score, ")"
                         }));
                        Log.Debug(string.Concat(new object[] { 
                            "Music Video DB - INSERT INTO 'files' (filename,artist,song,playcount,duration,rating,score) VALUES (", '"', rawVideos[i].Path, '"', ",", '"', str2, '"', ",", '"', rawVideos[i].Label, '"', ",", rawVideos[i].playcount, ",", '"', 
                            num2, '"', ",", rawVideos[i].rating, ",", rawVideos[i].score, ")"
                         }), new object[0]);
                        if (!GUIWindowManager.Initalized)
                        {
                            this.bgw.ReportProgress(1, "Added (song) " + str2 + " - " + rawVideos[i].Label);
                        }
                        if (!GUIWindowManager.Initalized)
                        {
                            userState.type = mVids.imgthreadJob.jobType.thumb;
                            userState.video = rawVideos[i].Path;
                            userState.width = Math.Round((double) (this.thumbHeight * num3), 0);
                        }
                        else
                        {
                            this.getThumb(rawVideos[i].Path, Math.Round((double) (this.thumbHeight * num3), 0));
                        }
                        Log.Debug("Music Video DB - {0} already in database ({0})", new object[] { str2 });
                    }
                    if (!GUIWindowManager.Initalized)
                    {
                        this.backgroundImageItems[rawVideos[i].jobNum] = new ManualResetEvent(false);
                        ThreadPool.QueueUserWorkItem(new WaitCallback(this.dispatchImgThreads), userState);
                    }
                }
                catch (Exception exception2)
                {
                    Log.Error("Music Video DB - Error Adding {0} to database {1}", new object[] { rawVideos[i].Path, exception2.ToString() });
                }
            }
            return true;
        }

        public bool addManualFile(string artist, bool isNew, string title, string file)
        {
            try
            {
                if (isNew)
                {
                    this.sqc.Execute(string.Concat(new object[] { "INSERT INTO 'artists' (artist) VALUES (", '"', artist, '"', ")" }));
                    Log.Debug(string.Concat(new object[] { "Music Video DB - INSERT INTO 'artists' (artist) VALUES (", '"', artist, '"', ")" }), new object[0]);
                }
                int d = 0;
                double aR = 1.7777;
                this.getInfo(file, out d, out aR);
                this.sqc.Execute(string.Concat(new object[] { 
                    "INSERT INTO 'files' (filename,artist,song,playcount,duration,rating,score) VALUES (", '"', file, '"', ",", '"', artist, '"', ",", '"', title, '"', ",", 0, ",", '"', 
                    d, '"', ",", 3, ",", 0, ")"
                 }));
                Log.Debug(string.Concat(new object[] { 
                    "Music Video DB - INSERT INTO 'files' (filename,artist,song,playcount,duration,rating,score) VALUES (", '"', file, '"', ",", '"', artist, '"', ",", '"', title, '"', ",", 0, ",", '"', 
                    d, '"', ",", 3, ",", 0, ")"
                 }), new object[0]);
                return true;
            }
            catch (Exception exception)
            {
                Log.Error("Music Video DB - Error adding file to database - " + exception.ToString(), new object[0]);
                return false;
            }
        }

        public void addMatch(string rawName, string matched)
        {
            this.sqc.Execute(string.Concat(new object[] { "INSERT INTO 'matches' (raw,matched) VALUES (", '"', rawName, '"', ",", '"', matched, '"', ")" }));
        }

        public bool addSingleFile(string file)
        {
            bool flag;
            Settings settings = new Settings(Config.GetFolder(Config.Dir.Config) + "/MediaPortal.xml", false);
            string str = settings.GetValue("movies", "extensions") + ",";
            string[] strArray = settings.GetValue("mvid", "basepath").Split("|".ToCharArray());
            string str2 = settings.GetValue("mvid", "masks");
            string[] strArray2 = settings.GetValue("mvid", "masks").Split("|".ToCharArray());
            List<string> list = new List<string>();
            List<string> list2 = new List<string>();
            if (str2 == "")
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            for (int i = 0; i < strArray.Length; i++)
            {
                if (Directory.Exists(strArray[i]))
                {
                    list.Add(strArray[i]);
                    if (flag)
                    {
                        list2.Add("IGNORE");
                    }
                    else
                    {
                        list2.Add(strArray2[i]);
                    }
                }
            }
            Log.Debug("paths {0} masks {1}", new object[] { list.Count.ToString(), list2.Count.ToString() });
            settings.Dispose();
            if (str.Contains(file.Substring(file.LastIndexOf("."))))
            {
                string[] strArray3 = file.Substring(file.LastIndexOf(@"\") + 1).Split("-".ToCharArray());
                List<mVids.musicVideo> rawVideos = new List<mVids.musicVideo>();
                mVids.musicVideo vid = new mVids.musicVideo {
                    artist = strArray3[0].Trim()
                };
                vid.Path = file;
                vid.Label = strArray3[1].Trim();
                foreach (string str3 in list)
                {
                    if (vid.Path.StartsWith(str3))
                    {
                        mVids.folderParser parser = new mVids.folderParser();
                        Log.Debug("about to parse folder with mask {0} folder {1}", new object[] { list2[list.IndexOf(str3)], str3 });
                        vid = parser.parseFolder(vid, list2[list.IndexOf(str3)], str3);
                        break;
                    }
                }
                rawVideos.Add(vid);
                if (this.Add(rawVideos))
                {
                    Log.Info("Music Video DB - Added new file " + file, new object[0]);
                    return true;
                }
            }
            return false;
        }

        public void checkDB()
        {
            if (this.sqc.Execute("SELECT name FROM sqlite_master WHERE type='table'").Rows.Count < 3)  //get_Rows().Count < 3)
            {
                this.sqc.Execute("CREATE TABLE artists (key INTEGER PRIMARY KEY, artist TEXT, lfmname TEXT, bio TEXT, content TEXT, tags TEXT, similar TEXT)");
                this.sqc.Execute("CREATE TABLE files (key integer primary key, filename TEXT, artist TEXT, song TEXT, playcount INT, rating INT, duration string, score int)");
                this.sqc.Execute("CREATE TABLE [matches] ([raw] TEXT NOT NULL COLLATE BINARY, [matched] TEXT NOT NULL COLLATE BINARY)");
                this.sqc.Execute("CREATE INDEX artist on artists (artist)");
                this.sqc.Execute("CREATE INDEX raw on matches (raw)");
            }
        }

        public string cleanupName(string rawName)
        {
            Settings settings = new Settings(Config.GetFolder(Config.Dir.Config) + "/MediaPortal.xml", false);
            string str = settings.GetValue("movies", "extensions") + ",";
            settings.Dispose();
            if (rawName.Contains(".") && str.Contains(rawName.Substring(rawName.LastIndexOf("."))))
            {
                rawName = rawName.Remove(rawName.LastIndexOf("."));
            }
            if (rawName.Contains("_"))
            {
                rawName = rawName.Replace("_", " ");
            }
            rawName = rawName.Trim();
            rawName = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(rawName);
            if (rawName.Contains("."))
            {
                string[] strArray = rawName.Split(".".ToCharArray());
                foreach (string str2 in strArray)
                {
                    if (str2.Length > 1)
                    {
                        rawName = rawName.Replace(".", " ");
                        return rawName;
                    }
                }
            }
            return rawName;
        }

        public bool clearDB()
        {
            try
            {
                this.sqc.Execute("DELETE FROM artists");
                this.sqc.Execute("DELETE FROM files");
                this.sqc.Execute("DELETE FROM matches");
                return true;
            }
            catch (Exception exception)
            {
                Log.Error("Music Video DB - Error clearing database - {0}", new object[] { exception.ToString() });
                return false;
            }
        }

        public bool delEntry(string filename)
        {
            try
            {
                this.sqc.Execute(string.Concat(new object[] { "DELETE FROM files WHERE filename=", '"', filename, '"' }));
                this.reconcileArtists();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public int delMissing()
        {
            Log.Info("Music Videos DB - Checking for and deleting missing files.", new object[0]);
            int num = 0;
            SQLiteResultSet set = this.sqc.Execute("SELECT filename,artist FROM files");
            for (int i = 0; i < set.Rows.Count; i++)
            {
                try
                {
                    if (!System.IO.File.Exists(set.Rows[i].fields[0].ToString()))
                    {
                        if (!GUIWindowManager.Initalized)
                        {
                            this.bgw.ReportProgress(1, "Remove reference to missing file:  " + set.Rows[i].fields[0].ToString());
                        }
                        this.sqc.Execute(string.Concat(new object[] { "DELETE FROM files WHERE filename=", '"', set.Rows[i].fields[0].ToString(), '"' }));
                        num++;
                    }
                }
                catch (Exception exception)
                {
                    if (!GUIWindowManager.Initalized)
                    {
                        this.bgw.ReportProgress(1, "Error removing reference to missing file:  " + set.Rows[i].fields[0].ToString());
                    }
                    Log.Error("Music Video DB - Error removing missing files, error={0}", new object[] { exception.ToString() });
                }
            }
            this.reconcileArtists();
            Log.Info("Music Videos DB - Removed {0} entries", new object[] { num });
            return num;
        }

        private void dispatchImgThreads(object info)
        {
            mVids.imgthreadJob job = (mVids.imgthreadJob) info;
            try
            {
                if (job.type == mVids.imgthreadJob.jobType.both)
                {
                    this.downloadArtistImage(job.aI);
                    this.getThumb(job.video, job.width);
                }
                if (job.type == mVids.imgthreadJob.jobType.thumb)
                {
                    this.getThumb(job.video, job.width);
                }
                this.backgroundImageItems[job.jobNumber].Set();
            }
            catch (Exception exception)
            {
                Log.Error("Music Video DB - Dispatch error {0}", new object[] { exception.ToString() });
                this.backgroundImageItems[job.jobNumber].Set();
            }
        }

        private void downloadArtistImage(mVids.artistInfo aI)
        {
            try
            {
                new WebClient().DownloadFile(aI.imageURL, Config.GetFolder(Config.Dir.Thumbs) + @"\mvids\Artists\" + aI.name + ".jpg");
                Log.Info("Music Video DB - Downloading image for {0}", new object[] { aI.name });
                Log.Debug("Music Video DB - Image URL is {0}", new object[] { aI.imageURL });
                if (!(GUIWindowManager.Initalized || this.bgw.CancellationPending))
                {
                    this.bgw.ReportProgress(1, "[img thread] Downloaded artist image for " + aI.name);
                }
            }
            catch (Exception exception)
            {
                if (!(GUIWindowManager.Initalized || this.bgw.CancellationPending))
                {
                    this.bgw.ReportProgress(1, "[img thread] ERROR downloading artist image for " + aI.name + ", check Configuration.log for details");
                }
                Log.Error("Music Video DB - ERROR downloading artist image for {0}, error=" + exception.ToString(), new object[] { aI.name });
            }
        }

        public bool fileInDB(string filename)
        {
            if (this.sqc.Execute(string.Concat(new object[] { "SELECT song FROM files WHERE filename=", '"', filename, '"' })).Rows.Count == 0)
            {
                return false;
            }
            return true;
        }

        public mVids.artistInfo getArtistInfo(string artist)
        {
            mVids.artistInfo info = new mVids.artistInfo();
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT lfmname,bio,content,tags FROM artists WHERE artist=", '"', artist, '"' }));
            List<string> list = new List<string>();
            info.name = artist;
            info.lfmName = set.Rows[0].fields[0];
            info.bioSummary = set.Rows[0].fields[1];
            info.bioContent = set.Rows[0].fields[2];
            info.tags = set.Rows[0].fields[3];
            return info;
        }

        public List<string> getArtists()
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT artist FROM artists ORDER BY artist");
            List<string> list = new List<string>();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.Add(set.Rows[i].fields[0].ToString());
            }
            return list;
        }

        public List<mVids.artist> getArtistsAndBios(bool summary)
        {
            SQLiteResultSet set = new SQLiteResultSet();
            if (summary)
            {
                set = this.sqc.Execute("SELECT artist,bio,tags FROM artists ORDER BY artist");
            }
            else
            {
                set = this.sqc.Execute("SELECT artist,content,tags FROM artists ORDER BY artist");
            }
            List<mVids.artist> list = new List<mVids.artist>();
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < set.Rows.Count; i++)
            {
              mVids.artist artist;
              artist = new mVids.artist
              {
                artistName = set.Rows[i].fields[0].ToString(),
                bio = set.Rows[i].fields[1].ToString(),
                tags = set.Rows[i].fields[2].ToString()
              };
              artist.vidCount = this.getArtistVideoCount(artist.artistName);
              artist.averageRating = this.getVideoRatingAverages(artist.artistName);
              list.Add(artist);
            }
            stopwatch.Stop();
            myLog.logger.log("Get artists took " + stopwatch.ElapsedMilliseconds.ToString());
            return list;
        }

        public List<string> getArtistsByGenre(string genre)
        {
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT artist FROM artists WHERE genre=", '"', genre, '"' }));
            List<string> list = new List<string>();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.Add(set.Rows[i].fields[0].ToString());
            }
            return list;
        }

        public List<string> getArtistsByStyle(string style)
        {
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT artist FROM artists WHERE styles LIKE ", '"', "%", style, "%", '"' }));
            List<string> list = new List<string>();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.Add(set.Rows[i].fields[0].ToString());
            }
            return list;
        }

        public List<string> getArtistsByTag(string tag)
        {
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT artist FROM artists WHERE tags LIKE ", '"', "%", tag, "%", '"' }));
            List<string> list = new List<string>();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.Add(set.Rows[i].fields[0].ToString());
            }
            return list;
        }

        public List<string> getArtistsByTone(string tone)
        {
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT artist FROM artists WHERE tones LIKE ", '"', "%", tone, "%", '"' }));
            List<string> list = new List<string>();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.Add(set.Rows[i].fields[0].ToString());
            }
            return list;
        }

        private int getArtistVideoCount(string artist)
        {
            return this.sqc.Execute("SELECT filename FROM files WHERE artist=\"" + this.resolveArtistName(artist) + "\"").Rows.Count;
        }

        public mVids.musicVideo getFileLabel(string path)
        {
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT artist,song FROM files WHERE filename=", '"', path, '"' }));
            mVids.musicVideo video = new mVids.musicVideo {
                artist = this.resolveLfmName(set.Rows[0].fields[0].ToString())
            };
            video.Label = video.artist + " - " + set.Rows[0].fields[1].ToString();
            return video;
        }

        public List<string> getFiles()
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT filename FROM 'files'");
            List<string> list = new List<string>();
            if (set.Rows.Count > 0)
            {
                for (int i = 0; i < set.Rows.Count; i++)
                {
                    list.Add(set.Rows[i].fields[0].ToString());
                }
            }
            return list;
        }

        public List<mVids.musicVideo> getFilesByArtist(string artist)
        {
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT filename,song,playcount,duration,rating,score FROM files WHERE artist=", '"', this.resolveArtistName(artist), '"', " ORDER BY song" }));
            List<string> list = this.getRankings();
            List<mVids.musicVideo> list2 = new List<mVids.musicVideo>();
            SQLiteResultSet set2 = this.sqc.Execute(string.Concat(new object[] { "SELECT tags FROM artists WHERE artist=", '"', artist, '"' }));
            for (int i = 0; i < set.Rows.Count; i++)
            {
                mVids.musicVideo item = new mVids.musicVideo {
                    artist = artist
                };
                item.Path = set.Rows[i].fields[0].ToString();
                item.Label = set.Rows[i].fields[1].ToString();
                item.playcount = set.Rows[i].fields[2].ToString();
                item.Duration = int.Parse(set.Rows[i].fields[3]);
                item.rating = set.Rows[i].fields[4].ToString();
                item.score = set.Rows[i].fields[5].ToString();
                item.ranking = ((list.IndexOf(item.Path) + 1)).ToString() + " / " + list.Count.ToString();
                if (set2.Rows.Count > 0)
                {
                    item.tags = set2.Rows[0].fields[0].ToString();
                }
                list2.Add(item);
            }
            Log.Debug("Music Video DB - Returning {0} files", new object[] { list2.Count.ToString() });
            return list2;
        }

        public List<mVids.musicVideo> getFilesByKeyOrder(int limit)
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT filename,artist,song,playcount,duration,rating,score FROM files ORDER BY key DESC LIMIT 0," + limit.ToString());
            List<string> list = this.getRankings();
            List<mVids.musicVideo> list2 = new List<mVids.musicVideo>();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                mVids.musicVideo item = new mVids.musicVideo();
                item.Path = set.Rows[i].fields[0].ToString();
                item.artist = this.resolveLfmName(set.Rows[i].fields[1].ToString());
                item.Label = item.artist + " - " + set.Rows[i].fields[2].ToString();
                item.playcount = set.Rows[i].fields[3].ToString();
                item.Duration = int.Parse(set.Rows[i].fields[4]);
                item.rating = set.Rows[i].fields[5].ToString();
                item.score = set.Rows[i].fields[6].ToString();
                item.ranking = ((list.IndexOf(item.Path) + 1)).ToString() + " / " + list.Count.ToString();
                list2.Add(item);
            }
            foreach (mVids.musicVideo video2 in list2)
            {
                set = this.sqc.Execute(string.Concat(new object[] { "SELECT tags FROM artists WHERE artist=", '"', video2.artist, '"' }));
                if (set.Rows.Count != 0)
                {
                    video2.tags = set.Rows[0].fields[0].ToString();
                }
                set = this.sqc.Execute(string.Concat(new object[] { "SELECT bio FROM artists WHERE artist=", '"', video2.artist, '"' }));
                if (set.Rows.Count != 0)
                {
                    video2.aI.bioContent = set.Rows[0].fields[0].ToString();
                }
            }
            return list2;
        }

        public List<mVids.musicVideo> getFilesByLastfmArtist(string lfmname)
        {
            string str;
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT artist FROM artists WHERE lfmname=", '"', lfmname, '"' }));
            if (set.Rows.Count > 0)
            {
                str = set.Rows[0].fields[0].ToString();
            }
            else
            {
                return null;
            }
            set = this.sqc.Execute(string.Concat(new object[] { "SELECT filename,song,playcount,duration,rating,score FROM files WHERE artist=", '"', str, '"', " ORDER BY song" }));
            Log.Debug(string.Concat(new object[] { "SELECT filename,song,playcount,duration,rating,score FROM files WHERE artist=", '"', str, '"', " ORDER BY song" }), new object[0]);
            List<string> list = this.getRankings();
            List<mVids.musicVideo> list2 = new List<mVids.musicVideo>();
            SQLiteResultSet set2 = this.sqc.Execute(string.Concat(new object[] { "SELECT tags FROM artists WHERE artist=", '"', str, '"' }));
            for (int i = 0; i < set.Rows.Count; i++)
            {
                mVids.musicVideo item = new mVids.musicVideo {
                    artist = str
                };
                item.Path = set.Rows[i].fields[0].ToString();
                item.Label = str + " - " + set.Rows[i].fields[1].ToString();
                item.playcount = set.Rows[i].fields[2].ToString();
                item.Duration = int.Parse(set.Rows[i].fields[3]);
                item.rating = set.Rows[i].fields[4].ToString();
                item.score = set.Rows[i].fields[5].ToString();
                item.ranking = ((list.IndexOf(item.Path) + 1)).ToString() + " / " + list.Count.ToString();
                if (set2.Rows.Count > 0)
                {
                    item.tags = set2.Rows[0].fields[0].ToString();
                }
                list2.Add(item);
            }
            Log.Debug("Music Video DB - Returning {0} files", new object[] { list2.Count.ToString() });
            return list2;
        }

        public List<mVids.musicVideo> getFilesByScore(int limit)
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT filename,artist,song,playcount,duration,rating,score FROM files ORDER BY score DESC LIMIT 0," + limit.ToString());
            List<mVids.musicVideo> list = new List<mVids.musicVideo>();
            List<string> list2 = this.getRankings();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                mVids.musicVideo item = new mVids.musicVideo();
                item.Path = set.Rows[i].fields[0].ToString();
                item.artist = this.resolveLfmName(set.Rows[i].fields[1].ToString());
                item.Label = item.artist + " - " + set.Rows[i].fields[2].ToString();
                item.playcount = set.Rows[i].fields[3].ToString();
                item.Duration = int.Parse(set.Rows[i].fields[4]);
                item.rating = set.Rows[i].fields[5].ToString();
                item.score = set.Rows[i].fields[6].ToString();
                item.ranking = ((list2.IndexOf(item.Path) + 1)).ToString() + " / " + list2.Count.ToString();
                list.Add(item);
            }
            foreach (mVids.musicVideo video2 in list)
            {
                set = this.sqc.Execute(string.Concat(new object[] { "SELECT tags FROM artists WHERE artist=", '"', video2.artist, '"' }));
                if (set.Rows.Count != 0)
                {
                    video2.tags = set.Rows[0].fields[0].ToString();
                }
                set = this.sqc.Execute(string.Concat(new object[] { "SELECT bio FROM artists WHERE artist=", '"', video2.artist, '"' }));
                if (set.Rows.Count != 0)
                {
                    video2.aI.bioContent = set.Rows[0].fields[0].ToString();
                }
            }
            return list;
        }

        public List<mVids.musicVideo> getFilesByTag(string tag)
        {
            SQLiteResultSet set;
            List<string> list = this.getArtistsByTag(tag);
            List<string> list2 = this.getRankings();
            List<mVids.musicVideo> list3 = new List<mVids.musicVideo>();
            foreach (string str in list)
            {
                set = this.sqc.Execute(string.Concat(new object[] { "SELECT filename,song,artist,playcount,duration,rating,score FROM files WHERE artist=", '"', this.resolveArtistName(str), '"', " ORDER BY artist" }));
                for (int i = 0; i < set.Rows.Count; i++)
                {
                    mVids.musicVideo item = new mVids.musicVideo();
                    item.Path = set.Rows[i].fields[0].ToString();
                    item.artist = this.resolveLfmName(set.Rows[i].fields[2].ToString());
                    item.Label = item.artist + " - " + set.Rows[i].fields[1].ToString();
                    item.playcount = set.Rows[i].fields[3].ToString();
                    item.Duration = int.Parse(set.Rows[i].fields[4]);
                    item.rating = set.Rows[i].fields[5].ToString();
                    item.score = set.Rows[i].fields[6].ToString();
                    item.ranking = ((list2.IndexOf(item.Path) + 1)).ToString() + " / " + list2.Count.ToString();
                    list3.Add(item);
                }
            }
            foreach (mVids.musicVideo video in list3)
            {
                set = this.sqc.Execute(string.Concat(new object[] { "SELECT bio FROM artists WHERE artist=", '"', video.artist, '"' }));
                if (set.Rows.Count != 0)
                {
                    video.aI.bioContent = set.Rows[0].fields[0].ToString();
                }
            }
            return list3;
        }

        public List<string> getGenres()
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT DISTINCT genre FROM artists ORDER BY genre");
            List<string> list = new List<string>();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.Add(set.Rows[i].fields[0].ToString());
            }
            return list;
        }

        public void getInfo(string file, out int d, out double AR)
        {
            MediaInfoLib.MediaInfo instance = MediaInfoLib.MediaInfo.GetInstance();
            instance.Open(file);
            try
            {
                string s = instance.getPlaytime();
                d = 0;
                int.TryParse(s, out d);
                Log.Debug("Music Video DB - " + file + " Duration=" + s, new object[0]);
            }
            catch (Exception exception)
            {
                d = 0;
                Log.Debug("Music Video DB - Error getting duration for {0}, error={1}", new object[] { file, exception.ToString() });
            }
            try
            {
                AR = double.Parse(instance.getAR());
                Log.Debug(string.Concat(new object[] { "Music Video DB - ", file, " AR=", (double) AR }), new object[0]);
            }
            catch (Exception exception2)
            {
                AR = 1.0;
                Log.Debug("Music Video DB - Error getting Aspect Ratio for {0}, error={1}", new object[] { file, exception2.ToString() });
            }
        }

        public List<string> getlfmArtists()
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT lfmname FROM artists ORDER BY lfmname");
            List<string> list = new List<string>();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.Add(set.Rows[i].fields[0].ToString());
            }
            return list;
        }

        public string getMatch(string rawName)
        {
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT matched FROM matches WHERE raw=", '"', rawName, '"' }));
            if (set.Rows.Count > 0)
            {
                if (!GUIWindowManager.Initalized)
                {
                    this.bgw.ReportProgress(1, rawName + " matches " + set.Rows[0].fields[0]);
                }
                return set.Rows[0].fields[0];
            }
            return "";
        }

        public List<PlayListItem> getRandomPlaylist()
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT filename,song,artist FROM files");
            List<PlayListItem> list = new List<PlayListItem>();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                PlayListItem item = new PlayListItem();
                item.FileName = set.Rows[i].fields[0].ToString();
                item.Type = PlayListItem.PlayListItemType.Video;
                item.Description = set.Rows[i].fields[2].ToString() + " - " + set.Rows[i].fields[1].ToString();
                list.Add(item);
            }
            return list;
        }

        public List<string> getRankings()
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT filename FROM files ORDER BY score DESC, artist");
            List<string> list = new List<string>();
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.Add(set.Rows[i].fields[0]);
            }
            return list;
        }

        public List<mVids.musicVideo> getSimilarVideos(string artist)
        {
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT similar FROM artists WHERE artist=", '"', artist, '"' }));
            List<mVids.musicVideo> list = new List<mVids.musicVideo>();
            List<string> list2 = new List<string>();
            list2.AddRange(set.Rows[0].fields[0].Split("|".ToCharArray()));
            list2.Sort();
            list2.Remove("");
            foreach (string str in list2)
            {
                List<mVids.musicVideo> collection = new List<mVids.musicVideo>();
                collection = this.getFilesByLastfmArtist(str);
                if (collection != null)
                {
                    list.AddRange(collection);
                }
            }
            return list;
        }

        public List<string> getSongInfo(string filename)
        {
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT * FROM 'files' WHERE filename=", '"', filename, '"' }));
            List<string> list = new List<string>();
            if (set.Rows.Count > 0)
            {
                list.Add(set.Rows[0].fields[4].ToString());
                list.Add(set.Rows[0].fields[5].ToString());
                list.Add(set.Rows[0].fields[6].ToString());
                list.Add(set.Rows[0].fields[7].ToString());
            }
            return list;
        }

        public List<mVids.musicVideo> getSongs()
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT filename,song,artist,rating,playcount,score,duration FROM 'files'");
            List<mVids.musicVideo> list = new List<mVids.musicVideo>();
            if (set.Rows.Count > 0)
            {
                for (int i = 0; i < set.Rows.Count; i++)
                {
                    mVids.musicVideo item = new mVids.musicVideo();
                    item.Label = set.Rows[i].fields[1].ToString();
                    item.Path = set.Rows[i].fields[0].ToString();
                    item.artist = set.Rows[i].fields[2].ToString();
                    item.rating = set.Rows[i].fields[3].ToString();
                    item.playcount = set.Rows[i].fields[4].ToString();
                    item.score = set.Rows[i].fields[5].ToString();
                    item.Duration = int.Parse(set.Rows[i].fields[6].ToString());
                    list.Add(item);
                }
            }
            return list;
        }

        public List<string> getStyles()
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT styles FROM artists");
            List<string> list = new List<string>();
            string str = ",";
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.AddRange(set.Rows[i].fields[0].Split(str.ToCharArray()));
            }
            List<string> list2 = new List<string>();
            foreach (string str2 in list)
            {
                if (!list2.Contains(str2.Trim()))
                {
                    list2.Add(str2.Trim());
                }
            }
            list2.Remove("");
            list2.Sort();
            return list2;
        }

        public List<string> getTags()
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT tags FROM artists");
            List<string> list = new List<string>();
            string str = "|";
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.AddRange(set.Rows[i].fields[0].Split(str.ToCharArray()));
            }
            List<string> list2 = new List<string>();
            foreach (string str2 in list)
            {
                if (!list2.Contains(str2.Trim()))
                {
                    list2.Add(str2.Trim());
                }
            }
            list2.Remove("");
            list2.Sort();
            return list2;
        }

        private void getThumb(string video, double width)
        {
            Settings settings = new Settings(Config.GetFolder(Config.Dir.Config) + "/MediaPortal.xml", true);
            string str = settings.GetValue("mvid", "thumbtime");
            string str2 = Config.GetFolder(Config.Dir.Log) + @"\mvids\Videos";
            string str3 = settings.GetValue("mvid", "ffmpeg");
            settings.Dispose();
            string str4 = video.Remove(0, video.LastIndexOf(@"\") + 1);
            if (!System.IO.File.Exists(str2 + @"\" + str4 + ".jpg"))
            {
                try
                {
                    Process process = new Process();
                    if ((width % 2.0) != 0.0)
                    {
                        width++;
                    }
                    Log.Debug("Music Video DB - ffmpeg command line= -i \"" + video + "\" -s " + width.ToString() + "*" + this.thumbHeight.ToString() + " -ss " + str + " -vframes 1 -f image2 -vcodec mjpeg \"" + str2 + @"\" + str4 + ".jpg\"", new object[0]);
                    process.StartInfo.Arguments = " -i \"" + video + "\" -s " + width.ToString() + "*" + this.thumbHeight.ToString() + " -ss " + str + " -vframes 1 -f image2 -vcodec mjpeg \"" + str2 + @"\" + str4 + ".jpg\"";
                    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    process.StartInfo.FileName = str3;
                    process.Start();
                    process.WaitForExit();
                    if (!(GUIWindowManager.Initalized || this.bgw.CancellationPending))
                    {
                        this.bgw.ReportProgress(1, "[img thread] Grabbed thumbnail from " + str4);
                    }
                }
                catch (Exception exception)
                {
                    Log.Error("Music Video DB - Error generating video thumbnail for {0}: {1}", new object[] { video, Environment.NewLine + exception.ToString() });
                }
            }
        }

        public List<string> getTones()
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT tones FROM artists");
            List<string> list = new List<string>();
            string str = ",";
            for (int i = 0; i < set.Rows.Count; i++)
            {
                list.AddRange(set.Rows[i].fields[0].Split(str.ToCharArray()));
            }
            List<string> list2 = new List<string>();
            foreach (string str2 in list)
            {
                if (!list2.Contains(str2.Trim()))
                {
                    list2.Add(str2.Trim());
                }
            }
            list2.Remove("");
            list2.Sort();
            return list2;
        }

        public int getVideoRatingAverages(string artist)
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT rating FROM files WHERE artist=\"" + this.resolveArtistName(artist) + "\"");
            int num = 0;
            for (int i = 0; i < set.Rows.Count; i++)
            {
                num += int.Parse(set.Rows[i].fields[0].ToString());
            }
            if (num > 0)
            {
                return Convert.ToInt32((int) (num / set.Rows.Count));
            }
            return 3;
        }

        public int incPlayCount(string filename)
        {
            SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT playcount,rating FROM files WHERE filename=", '"', filename, '"' }));
            this.sqc.Execute(string.Concat(new object[] { "UPDATE files set playcount=", '"', int.Parse(set.Rows[0].fields[0]) + 1, '"', ", score=", (int.Parse(set.Rows[0].fields[0]) + 1) * int.Parse(set.Rows[0].fields[1]), " where filename=", '"', filename, '"' }));
            return (int.Parse(set.Rows[0].fields[0]) + 1);
        }

        public void reconcileArtists()
        {
            Log.Info("Music Videos DB - Reconciling artists", new object[0]);
            SQLiteResultSet set = this.sqc.Execute("SELECT lfmname FROM artists");
            foreach (SQLiteResultSet.Row row in set.Rows)
            {
                try
                {
                    if (this.sqc.Execute(string.Concat(new object[] { "SELECT * FROM files WHERE artist=", '"', row.fields[0], '"' })).Rows.Count == 0)
                    {
                        this.sqc.Execute(string.Concat(new object[] { "DELETE FROM artists WHERE artist=", '"', row.fields[0], '"' }));
                        this.sqc.Execute(string.Concat(new object[] { "DELETE FROM matches WHERE matched=", '"', row.fields[0], '"' }));
                    }
                }
                catch (Exception exception)
                {
                    Log.Error("Music Video DB - Error ocurred during artist reconciliation, error={0}", new object[] { exception.ToString() });
                }
            }
        }

        public void removeSingle(string filename)
        {
            this.sqc.Execute(string.Concat(new object[] { "DELETE FROM files WHERE filename=", '"', filename, '"' }));
            this.reconcileArtists();
        }

        public string resolveArtistName(string artist)
        {
            SQLiteResultSet set = this.sqc.Execute("SELECT lfmname FROM artists WHERE artist=\"" + artist + "\"");
            if (set.Rows.Count > 0)
            {
                return set.Rows[0].fields[0].ToString();
            }
            return "";
        }

        private string resolveLfmName(string lfmName)
        {
            myLog.logger.log("SELECT artist FROM artists WHERE lfmname=\"" + lfmName + "\"");
            SQLiteResultSet set = this.sqc.Execute("SELECT artist FROM artists WHERE lfmname=\"" + lfmName + "\"");
            if (set.Rows.Count > 0)
            {
                return set.Rows[0].fields[0].ToString();
            }
            return "";
        }

        public void setRating(string filename, int rating)
        {
            try
            {
                SQLiteResultSet set = this.sqc.Execute(string.Concat(new object[] { "SELECT playcount FROM files WHERE filename=", '"', filename, '"' }));
                this.sqc.Execute(string.Concat(new object[] { "UPDATE files SET rating=", rating, ", score=", int.Parse(set.Rows[0].fields[0]) * rating, " WHERE filename=", '"', filename, '"' }));
            }
            catch (Exception exception)
            {
                Log.Error("Music Video DB - Error setting rating of {0} to {1}, error={2}", new object[] { rating.ToString(), filename, exception.ToString() });
            }
        }

        public bool updateArtist(string artist, string oldArtist, string lfmname, string oldlfmname, string bio, string content, string tags, string spacer)
        {
            try
            {
                if (oldlfmname != lfmname)
                {
                    List<mVids.musicVideo> list = this.getFilesByArtist(oldArtist);
                    foreach (mVids.musicVideo video in list)
                    {
                        Log.Debug("Music Video DB - Setting the following fields " + video.Path, new object[] { video.Path, lfmname, video.Label, video.playcount, video.rating, video.Duration.ToString(), video.score });
                        this.updateEntireRecord(video.Path, video.Path, lfmname, video.Label, video.playcount, video.rating, video.Duration.ToString(), video.score);
                    }
                    this.sqc.Execute(string.Concat(new object[] { "UPDATE matches SET matched=", '"', lfmname, '"', " WHERE matched=", '"', oldlfmname, '"' }));
                }
                this.sqc.Execute(string.Concat(new object[] { 
                    "UPDATE artists SET artist=", '"', artist, '"', ", lfmname=\"", lfmname, "\", bio= ", '"', bio, '"', ", content=", '"', content, '"', ", tags=", '"', 
                    tags, '"', " WHERE artist=", '"', oldArtist, '"'
                 }));
                return true;
            }
            catch (Exception exception)
            {
                Log.Error("Music Video DB - Error updating {0}, {1}", new object[] { artist, exception.ToString() });
                return false;
            }
        }

        public bool updateEntireRecord(string originalFilename, string filename, string artist, string song, string playcount, string rating, string duration, string score)
        {
            try
            {
                Log.Debug(string.Concat(new object[] { 
                    "UPDATE files SET filename=", '"', filename, '"', ", song=", '"', song, '"', ", playcount= ", '"', playcount, '"', ", rating=", '"', rating, '"', 
                    ", duration=", '"', duration, '"', ", score=", '"', score, '"', ", artist=", '"', artist, '"', " WHERE filename=", '"', originalFilename, '"'
                 }), new object[0]);
                this.sqc.Execute(string.Concat(new object[] { 
                    "UPDATE files SET filename=", '"', filename, '"', ", song=", '"', song, '"', ", playcount= ", '"', playcount, '"', ", rating=", '"', rating, '"', 
                    ", duration=", '"', duration, '"', ", score=", '"', score, '"', ", artist=", '"', artist, '"', " WHERE filename=", '"', originalFilename, '"'
                 }));
                return true;
            }
            catch (Exception exception)
            {
                Log.Error("Music Video DB - Error at updateEntireRecord - " + exception.ToString(), new object[0]);
                return false;
            }
        }

        public void updateLength(string filename, int length)
        {
            try
            {
                this.sqc.Execute(string.Concat(new object[] { "UPDATE files SET duration=", '"', length.ToString(), '"', " WHERE filename=", '"', filename, '"' }));
            }
            catch
            {
                Log.Error("Music Video DB - Error setting length {0} for {1}", new object[] { length.ToString(), filename });
            }
        }

        public void updateRecord(string oldname, string newname)
        {
            this.sqc.Execute(string.Concat(new object[] { "UPDATE files SET filename=", '"', newname, '"', " WHERE filename=", '"', oldname, '"' }));
        }

        public void WaitAll()
        {
            Thread.Sleep(0x7d0);
            ManualResetEvent[] backgroundImageItems = this.backgroundImageItems;
            for (int i = 0; i < backgroundImageItems.Length; i++)
            {
                WaitHandle handle = backgroundImageItems[i];
                if (handle != null)
                {
                    Log.Debug("Music Video DB - Waiting for imgjob", new object[0]);
                    handle.WaitOne(0x2710, true);
                }
            }
        }
    }
}

